#-------------------------------------------------------------------------------
# Name:        VolumeSlider
# Purpose:
#
# Author:      Pablo
#
# Created:     10/07/2012
# Copyright:   (c) Monxcleyr Productions 2012
# Licence:     GPL v3
#-------------------------------------------------------------------------------
#!/usr/bin/env python

import pygame, os
from ..gui import button, label

class VolumeSlider():

  def __init__(self, pos, volume, enabled, surface, sound, soundfile, hoversound, common, debug, title, font, fontcolor):
    self.pos = pos
    self.volume = volume
    if self.volume % 5 != 0:
      self.tempvolume = self.volume / 5 + 1
    else:
      self.tempvolume = self.volume / 5
    self.enabled = enabled
    self.surface = surface
    self.sound = sound
    self.hoversound = hoversound
    self.soundfile = soundfile
    self.common = common
    self.debug = debug
    self.title = title
    self.font = font
    self.fontcolor = fontcolor
    self.box = pygame.image.load(os.path.join('resources/graphics/options/volumebox.bmp'))
    self.block = pygame.image.load(os.path.join('resources/graphics/options/volumeblock.bmp'))
    self.arrow = pygame.image.load(os.path.join('resources/graphics/options/volumearrow.bmp'))
    self.arrowhover = pygame.image.load(os.path.join('resources/graphics/options/volumearrowhover.bmp'))
    self.box.set_colorkey((255,0,255))
    self.block.set_colorkey((255,0,255))
    self.arrow.set_colorkey((255,0,255))
    self.arrowhover.set_colorkey((255,0,255))
    self.arrowleft = pygame.transform.rotate(self.arrow, 180)
    self.arrowlefthover = pygame.transform.rotate(self.arrowhover, 180)

    total = self.arrow.get_width() + 1 + self.box.get_width() + 1 + self.arrow.get_width()
    total = total / 2
    self.pos = (self.pos[0] - total, self.pos[1])

    self.arrowpos = self.pos[0] + self.box.get_width() + self.arrow.get_width() + 2
    self.boxpos = self.pos[0] + self.arrow.get_width() + 1
    self.arrowrect = self.getRect(self.arrowpos, self.arrow)
    self.arrowleftrect = self.getRect(self.pos[0], self.arrow)
    self.blockpos = self.boxpos + 3

    boxheight = self.box.get_height()
    self.mutebutton = button.Checkbox(self.surface, (self.boxpos, self.pos[1] + boxheight + 3), self.font, '')
    if self.enabled == False:
      self.mutebutton.setState(1)
    self.label = label.Label(self.surface, 'Mute', (self.boxpos + 22, self.pos[1] + boxheight + 5), 'left', self.font, self.fontcolor)
    self.title = label.Label(self.surface, self.title, (self.boxpos, self.pos[1] - 13), 'left', self.font, self.fontcolor)

    self.upclicked, self.downclicked = False, False
    self.prevhover = False

  def draw(self, mouse):

    self.surface.blit(self.box, (self.boxpos, self.pos[1]))
    self.surface.blit(self.arrowleft, self.pos)
    self.surface.blit(self.arrow, (self.arrowpos, self.pos[1]))

    # Draw hovered buttons
    hover = False
    if self.arrowrect.collidepoint(mouse[0]):
      self.surface.blit(self.arrowhover, (self.arrowpos, self.pos[1]))
      hover = True
      if mouse[1] == 1:
        self.upclicked = True
        self.sound.play(self.soundfile, self.common.settings)
    if self.arrowleftrect.collidepoint(mouse[0]):
      self.surface.blit(self.arrowlefthover, self.pos)
      hover = True
      if mouse[1] == 1:
        self.sound.play(self.soundfile, self.common.settings)
        self.downclicked = True

    if hover:
      if not self.prevhover:
        self.sound.play(self.hoversound, self.common.settings)
        self.prevhover = True
    else:
      self.prevhover = False

    if self.upclicked == True and self.tempvolume != 20:
      self.tempvolume += 1
      self.debug.add('Volume: ' + str(self.tempvolume))
    if self.downclicked == True and self.tempvolume != 0:
      self.tempvolume -= 1
      self.debug.add('Volume: ' + str(self.tempvolume))
    self.upclicked, self.downclicked = False, False

    if mouse[1] == 1:
      temp = 0
    else:
      temp = 1
    self.mutebutton.draw(mouse[0], mouse[1], temp)


    if self.mutebutton.state() == 0:
      if self.enabled == False:
        self.debug.add('Unmuted')
      self.enabled = True
      for i in range(0, self.tempvolume):
        self.surface.blit(self.block, (self.blockpos + (6 * i), self.pos[1] + 3))
    else:
      if self.enabled == True:
        self.debug.add('Muted')
      self.enabled = False

    self.label.draw()
    self.title.draw()


  def getRect(self, pos, image):
    return pygame.Rect((pos, self.pos[1]), (image.get_width(), image.get_height()))

  def getVolume(self):
      return self.tempvolume, self.enabled

